#include "Menu.h"
#include "ui_Menu.h"
#include <QDebug>
#include <QDateTime>
#include <QRegularExpression>
#include "Login.h"
#include "ui_FromMyDialog.h"
Menu::Menu(MWidget *parent) :
    MyDialog(parent),
    ui(new Ui::Menu)
{
    ui->setupUi(this);
    dialogName=MyDialog::MenuDialog;
    if(this->bedDialog==nullptr)
    {
        this->bedDialog=new FromMyDialog;
        QVBoxLayout *layout = new QVBoxLayout;
        this->bed_=new Bed();
        layout->addWidget(this->bed_);
        this->bedDialog->ui->downWidget->setLayout(layout);
        connect(this->bed_,&Bed::showSiri_signal,this->bedDialog,&FromMyDialog::showSiri_slot);
        connect(this->bed_,&Bed::showPatient_signal,this->bedDialog,&FromMyDialog::showPatient_slot);
        connect(this->bed_,&Bed::siriWork_signal,this,&Menu::siriWork);//siriWork_slot
    }

    if(this->lineDialog==nullptr)
    {
        this->lineDialog=new FromMyDialog;
        QVBoxLayout *layout = new QVBoxLayout;
        this->line_=new Line;
        layout->addWidget(this->line_);
        this->lineDialog->ui->downWidget->setLayout(layout);
        connect(this->line_,&Line::showSiri_signal,this->lineDialog,&FromMyDialog::showSiri_slot);
        connect(this->line_,&Line::showPatient_signal,this->lineDialog,&FromMyDialog::showPatient_slot);
        connect(this->line_,&Line::siriWork_signal,this,&Menu::siriWork);//siriWork_slot
    }

    if(this->operDialog==nullptr)
    {
        this->operDialog=new FromMyDialog;
        QVBoxLayout *layout = new QVBoxLayout;
        this->oper_=new Operator;
        layout->addWidget(this->oper_);
        this->operDialog->ui->downWidget->setLayout(layout);
        connect(this->oper_,&Operator::showSiri_signal,this->operDialog,&FromMyDialog::showSiri_slot);
        connect(this->oper_,&Operator::showPatient_signal,this->operDialog,&FromMyDialog::showPatient_slot);
        connect(this->oper_,&Operator::siriWork_signal,this,&Menu::siriWork);//siriWork_slot
    }

    if(this->setDialog==nullptr)
    {
        this->setDialog=new FromMyDialog;
        QVBoxLayout *layout = new QVBoxLayout;
        this->set_=new Set;
        layout->addWidget(this->set_);
        this->setDialog->ui->downWidget->setLayout(layout);
        connect(this->set_,&Set::showSiri_signal,this->setDialog,&FromMyDialog::showSiri_slot);
        connect(this->set_,&Set::showPatient_signal,this->setDialog,&FromMyDialog::showPatient_slot);
        connect(this->set_,&Set::siriWork_signal,this,&Menu::siriWork);//siriWork_slot
    }

    connect(ui->bed,&MyLabel::clicked,this,&Menu::createModule);
    connect(ui->line,&MyLabel::clicked,this,&Menu::createModule);
    connect(ui->set,&MyLabel::clicked,this,&Menu::createModule);
    connect(ui->operation,&MyLabel::clicked,this,&Menu::createModule);
    connect(ui->siri,&MyLabel::clicked,this,&Menu::createModule);
    connect(ui->camera,&MyLabel::clicked,this,&Menu::createModule);
    connect(this,&Menu::sendServerData_signal,Login::myTCPClient,&MyTcp::sendServerData_slot);
    connect(Login::myTCPClient->getTask(),&Task::receiveServerData_signal,this,&Menu::receiveServerData_slot);
    emit sendServerData_signal("7E7E;kind:surgery;function:02;",true);
    this->installEventFilter(this);
}

Menu::~Menu()
{
    delete ui;
}

void Menu::createModule(QString toolTip)
{
    qDebug()<<"toolTip"<<toolTip<<endl;
    //if(FromMyDialog::siri->isVisible())
        FromMyDialog::siri->hideSiri();
    //else
    {
        if(toolTip=="bed")
        {
            this->bedDialog->show();
        }
        else if(toolTip=="line")
        {
            this->line_->init();
            this->lineDialog->show();
        }
        else if(toolTip=="set")
        {
            //..
            this->setDialog->show();
        }
        else if(toolTip=="operation")
        {
            //..
            this->operDialog->show();
        }
        else if(toolTip=="siri")
        {
            //FromMyDialog::siri->showSiri();
           emit this->showSiri_signal();
            //..
        }
        else if(toolTip=="camera")
        {
            //..
        }
        }
}

FromMyDialog *Menu::getOperDialog()
{
    return this->operDialog;
}

FromMyDialog *Menu::getBedDialog()
{
    return this->bedDialog;
}

FromMyDialog *Menu::getLineDialog()
{
    return this->lineDialog;
}

FromMyDialog *Menu::getsetDialog()
{
    return this->setDialog;
}

bool Menu::eventFilter(QObject *watched, QEvent *event)
{
    return QObject::eventFilter(watched, event);
}

void Menu::receiveServerData_slot(int function, QMap<QString, QString> kv)
{qDebug()<<"kv.value(people):"<<kv.value("people")<<endl;
    switch (function) {
    case 2:
        //this->oper_->getCollect()->setCurrentCollectNumber(kv.value("currentCollectNumber").toInt());
        analysisPatients(kv.value("people"));
        break;
//    case 4:
//    {
//        QMutexLocker locker(&Menu::patientsLock);
//            for(int i=0;i<Menu::patients.size();i++)
//            {
//                if(Menu::patients[i].id==kv.value("id"))
//                {
//                    this->patients.removeAt(i);
//                    break;
//                }
//            }
//    }
//        break;
     case 5:
    {qDebug()<<"------------------------------------"<<endl;
       // QMutexLocker locker(&Menu::patientsLock);
        for(int i=0;i<Menu::patients.size();i++)
        {
            if(Menu::patients[i].phone==kv.value("phone"))
            {
                this->patients.removeAt(i);
                break;
            }
        }
        if(this->line_!=nullptr && this->line_->isActiveWindow())
            this->line_->init();
    }
        break;
    }
}

void Menu::siriWork(MyDialogName dialogName,functionFlag flag)
{//对功能分类
    qDebug()<<"siriWork_slot:"<<flag<<endl;
    switch (flag) {
    case functionFlag::clear_data:
    break;
    case functionFlag::light_openhalf:
    case functionFlag::light_close:
    case functionFlag::light_openall:
    case functionFlag::bed_1:
    case functionFlag::bed_2:
    case functionFlag::bed_3:
    case functionFlag::bed_4:
        this->bed_->siriWork(dialogName,flag);
    break;
    case functionFlag::pump_start:
    case functionFlag::pump_stop:
        this->oper_->siriWork(dialogName,flag);
        if(this->isActiveWindow())
            FromMyDialog::siri->hideSiri();//隐藏菜单的siri显示
        this->operDialog->show();
        this->operDialog->showSiri();//显示手术界面的siri
    break;
    case functionFlag::call_yes:
        emit this->showPatient_signal(MyDialog::showPatientInfor);
        this->bed_->siriWork(dialogName,flag);
        this->oper_->siriWork(dialogName,flag);
        this->set_->siriWork(dialogName,flag);
        break;
    case functionFlag::call_cancel:
        if(dialogName==MyDialogName::LineDialog)
        {
            this->bed_->siriWork(dialogName,flag);
            this->oper_->siriWork(dialogName,flag);
            this->set_->siriWork(dialogName,flag);
        }
        else if(dialogName==MyDialogName::BedDialog)
        {
            this->line_->siriWork(dialogName,flag);
            this->oper_->siriWork(dialogName,flag);
            this->set_->siriWork(dialogName,flag);
        }
        else if(dialogName==MyDialogName::OperationDialog)
        {
            this->line_->siriWork(dialogName,flag);
            this->bed_->siriWork(dialogName,flag);
            this->set_->siriWork(dialogName,flag);
        }
        else if(dialogName==MyDialogName::MenuDialog)
        {
            this->line_->siriWork(dialogName,flag);
            this->bed_->siriWork(dialogName,flag);
            this->oper_->siriWork(dialogName,flag);
            this->set_->siriWork(dialogName,flag);
        }
        else if(dialogName==MyDialogName::setDialog)
        {
            this->line_->siriWork(dialogName,flag);
            this->bed_->siriWork(dialogName,flag);
            this->oper_->siriWork(dialogName,flag);
        }
        this->showPatient_signal("");
    break;
    }
}

void Menu::analysisPatients(QString information)
{qDebug()<<"information："<<information<<endl;
    QRegularExpression reg0("(?<=\\[).*?(?=\\])");
    QRegularExpressionMatchIterator iter = reg0.globalMatch(information);

    QRegularExpression reg1("(?<=\\,)[^,:]*?(?=\\:)");
    QRegularExpressionMatchIterator iterKey;
    QRegularExpression reg2("(?<=\\:)[^,:]*?(?=\\,)");
    QRegularExpressionMatchIterator iterValue;

    while (iter.hasNext())
    {
        QString str=","+iter.next().captured(0)+",";
        iterKey= reg1.globalMatch(str);
        iterValue=reg2.globalMatch(str);

        QMap<QString,QString> kv;
        while (iterKey.hasNext() && iterValue.hasNext())
            kv.insert(iterKey.next().captured(0),iterValue.next().captured(0));

        Information inf={
            .name=kv.value("name"),
            .sex=kv.value("sex"),
            .phone=kv.value("phone"),
            .nation=kv.value("nation"),
            .birth=kv.value("birth"),
            .age=kv.value("age").toInt(),
            .callNumber=kv.value("number"),
            .blood=kv.value("blood"),
            .id=kv.value("id"),
            .illness=kv.value("illness"),
            .irritability=kv.value("irritability"),//曹谨鹏知识：要按照顺序排列，否则会报错?
        };
        this->patients.append(inf);
    }
    if(this->line_!=nullptr && this->line_->isActiveWindow())
        this->line_->init();
}


QList<Information> Menu::patients;
QMutex Menu::patientsLock;
